package LogicSide;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.extensions.Rating;
import com.google.gdata.data.geo.impl.GeoRssWhere;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaPlayer;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YouTubeMediaContent;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeMediaRating;
import com.google.gdata.data.youtube.YtPublicationState;
import com.google.gdata.data.youtube.YtStatistics;
import com.google.gdata.util.ServiceException;

public class SongResult {
	public static void main(String[] args) throws FileNotFoundException,
			IOException, ServiceException {
		getLink("die antwoord");
	}

	public static void getLink(String songname) throws FileNotFoundException,
			IOException, ServiceException {

		// authentication
		YouTubeService service = new YouTubeService(
				"AI39si4zCtqq6lXCMxUAv08KR3vzGE1niRG_hHJITp0lIUy_KeaY0tTJdMgxX6yMy9x5bdAQbjFgD2wxQw4kxCNp13xfG2gpQQ");
		YouTubeQuery query = new YouTubeQuery(new URL(
				"http://gdata.youtube.com/feeds/api/videos"));

		// order results by the relevance of the videos
		query.setOrderBy(YouTubeQuery.OrderBy.RELEVANCE);

		// search for songname
		query.setFullTextQuery(songname);

		VideoFeed videoFeed = service.query(query, VideoFeed.class);
		printVideoFeed(videoFeed, true);
	}

	public static void printVideoFeed(VideoFeed videoFeed, boolean detailed) {
		VideoEntry videoEntry = videoFeed.getEntries().get(0);
		printVideoEntry(videoEntry, detailed);
	}

	public static void printVideoEntry(VideoEntry videoEntry, boolean detailed) {
		System.out.println("Title: " + videoEntry.getTitle().getPlainText());

		if (videoEntry.isDraft()) {
			System.out.println("Video is not live");
			YtPublicationState pubState = videoEntry.getPublicationState();
			if (pubState.getState() == YtPublicationState.State.PROCESSING) {
				System.out.println("Video is still being processed.");
			} else if (pubState.getState() == YtPublicationState.State.REJECTED) {
				System.out.print("Video has been rejected because: ");
				System.out.println(pubState.getDescription());
				System.out.print("For help visit: ");
				System.out.println(pubState.getHelpUrl());
			} else if (pubState.getState() == YtPublicationState.State.FAILED) {
				System.out.print("Video failed uploading because: ");
				System.out.println(pubState.getDescription());
				System.out.print("For help visit: ");
				System.out.println(pubState.getHelpUrl());
			}
		}

		if (videoEntry.getEditLink() != null) {
			System.out.println("Video is editable by current user.");
		}

		if (detailed) {

			YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();

			System.out.println("Uploaded by: " + mediaGroup.getUploader());

			System.out.println("Video ID: " + mediaGroup.getVideoId());
			System.out.println("Description: "
					+ mediaGroup.getDescription().getPlainTextContent());

			MediaPlayer mediaPlayer = mediaGroup.getPlayer();
			System.out.println("Web Player URL: " + mediaPlayer.getUrl());
			MediaKeywords keywords = mediaGroup.getKeywords();
			System.out.print("Keywords: ");
			for (String keyword : keywords.getKeywords()) {
				System.out.print(keyword + ",");
			}

			GeoRssWhere location = videoEntry.getGeoCoordinates();
			if (location != null) {
				System.out.println("Latitude: " + location.getLatitude());
				System.out.println("Longitude: " + location.getLongitude());
			}

			Rating rating = videoEntry.getRating();
			if (rating != null) {
				System.out.println("Average rating: " + rating.getAverage());
			}

			YtStatistics stats = videoEntry.getStatistics();
			if (stats != null) {
				System.out.println("View count: " + stats.getViewCount());
			}
			System.out.println();

			System.out.println("\tThumbnails:");
			for (MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) {
				System.out.println("\t\tThumbnail URL: "
						+ mediaThumbnail.getUrl());
				System.out.println("\t\tThumbnail Time Index: "
						+ mediaThumbnail.getTime());
				System.out.println();
			}

			System.out.println("\tMedia:");
			for (YouTubeMediaContent mediaContent : mediaGroup
					.getYouTubeContents()) {
				System.out.println("\t\tMedia Location: "
						+ mediaContent.getUrl());
				System.out.println("\t\tMedia Type: " + mediaContent.getType());
				System.out.println("\t\tDuration: "
						+ mediaContent.getDuration());
				System.out.println();
			}

			for (YouTubeMediaRating mediaRating : mediaGroup
					.getYouTubeRatings()) {
				System.out
						.println("Video restricted in the following countries: "
								+ mediaRating.getCountries().toString());
			}

			showClip(mediaPlayer.getUrl());
		}
	}

	public static String getSongYoutubeURL(String songName) throws IOException, ServiceException {
		// authentication
		YouTubeService service = new YouTubeService(
				"AI39si4zCtqq6lXCMxUAv08KR3vzGE1niRG_hHJITp0lIUy_KeaY0tTJdMgxX6yMy9x5bdAQbjFgD2wxQw4kxCNp13xfG2gpQQ");
		YouTubeQuery query = new YouTubeQuery(new URL(
				"http://gdata.youtube.com/feeds/api/videos"));

		// order results by the relevance of the videos
		query.setOrderBy(YouTubeQuery.OrderBy.RELEVANCE);

		// search for songname
		query.setFullTextQuery(songName);

		VideoFeed videoFeed = service.query(query, VideoFeed.class);
		
		
		String video_id = videoFeed.getEntries().get(0).getId();
		String[] video_id_parts = video_id.split(":");
		String id = video_id_parts[video_id_parts.length - 1];
		//String youtubeURL = videoFeed.getEntries().get(0).getMediaGroup().getPlayer().getUrl();
		
		String embeddedPlayerURL = "http://www.youtube.com/embed/" + id;
		return embeddedPlayerURL;
	}

	public static void showClip(String url) {
		if (!java.awt.Desktop.isDesktopSupported()) {

			System.err.println("Desktop is not supported (fatal)");
			System.exit(1);
		}

		java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
		if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {

			System.err
					.println("Desktop doesn't support the browse action (fatal)");
			System.exit(1);
		}

		try {

			java.net.URI uri = new java.net.URI(url);
			desktop.browse(uri);
		} catch (Exception e) {

			System.err.println(e.getMessage());
		}
	}

}
